<template>
    <modal
        v-bind="modalProps"
        @confirm="confirm"
        @cancel="cancel"
        @done="$emit('done', lastRes)"
    >
        <div class="alert alert-dismissible fade text-center alert-warning" role="alert" :class="[infoShow ? 'show' : '']"
            style="position: absolute; top: 0; left: 0; z-index: 1; width: 100%;">
            <strong>{{info}}</strong>
            <button type="button" class="close" @click.stop="infoShow=false;">
                <span aria-hidden="true">&times;</span>
            </button>
        </div>
        <directory
            ref="directory"
            @fresh="setDir"
            :editable="false"
            style="height: 60vh"
        ></directory>
    </modal>
</template>
<script type="text/javascript">
import directory from './directory'
import ajaxModal from '../plugin/ajaxModal'
import {defaultModal as modal} from '../plugin/ajaxModal'
import $ from 'jquery'
import Vue from 'vue'

var constant = {
    FORBID_MOVE_TO_CHILDREN: "不能移动到子文件夹"
}


export default {
    props: {
        'doc': {required: true},
        'args': {default() { return {}; }},
        'child_id': {default() { return []; }},
        isCopy: {type: Boolean, default: false}
    },
    mixins: [ajaxModal],
    components: {modal, directory},
    data() {
        return {
            target: null,
            info: null,
            infoShow: false
        }
    },
    computed: {
        isSendValid() {
            return !this.isLoading &&
            this.target != null && this.target._id != this.doc._id &&
            (this.$refs.directory && !this.$refs.directory.isLoading);
        }
    },
    methods: {
        setDir(doc) {
            this.target = doc;
        },
        getParams() {
            var vm = this, args = $.isFunction(vm.args) ? vm.args() : vm.args || {};
            return {
                _id: vm.doc._id,
                args: Object.assign({
                    child_id: vm.child_id,
                    dest_id: vm.target._id
                }, args)
            }
        }
    },
    watch: {
    },
    created() {
        var vm = this;
        this.$nextTick(() => {
            var originalMethods = vm.$refs.directory.itemClick;
            vm.$refs.directory.itemClick = function(doc) {
                if (vm.child_id.indexOf(doc._id) != -1) {
                    vm.info = constant.FORBID_MOVE_TO_CHILDREN;
                    vm.infoShow = true;
                    return;
                } else {
                    return originalMethods.apply(vm.$refs.directory, arguments);
                }
            }
        })
    }
}
</script>
<style type="text/css" scoped>
    
</style>